﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Help;
using Model;

namespace BLLA
{
    public partial class Order_MerSend_T : BaseBLL<Model.Order_MerSend_T>, IBLL.IOrder_MerSend_TBLL
    {
        ObjectSerialize os = new ObjectSerialize();
        DataResult result = new DataResult();
        /// <summary>
        /// 这个商户有哪个地址可以送 
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="MerId"></param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string GetSendAddFromMerId(int pageIndex, int pageSize, string MerId, string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion
                WriteLog.WriteW(" 这个商户有哪个地址可以送：1" + MerId);
                #region 执行操作
                List<Model.View_Order_MerSend> ls = base.DBSession.IView_Order_MerSendDAL.GetListBy("select * from View_Order_MerSend where  Stat=1 and MerId='" + MerId + "'");
                WriteLog.WriteW(" 这个商户有哪个地址可以送：2" + MerId);
                if (ls != null & ls.Count > 0)
                {
                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.total = ls.Count;
                    result.rows = ls.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }
                #endregion
                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW(" 这个商户有哪个地址可以送失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
        }

        public string UpdateMerSendAdd(string MerId, string AddIds, string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion
                #region 执行操作
                base.DelBy(u => u.MerId == MerId);
                if (AddIds.Trim() == "")
                {
                    result.BS = "1";
                    result.Msg = " 成功";
                    return os.StringOfObject(result, RETID);
                }
                List<Model.Order_MerSend_T> ls = new List<Model.Order_MerSend_T>();
                string[] id = AddIds.Split(',');
                for (int i = 0; i < id.Length; i++)
                {
                    ls.Add(new Model.Order_MerSend_T() { MerId = MerId, Stat = 1, MerSendAddId = int.Parse(id[i]) });
                }
                if (base.Add(ls) > 0)
                {
                    result.BS = "1";
                    result.Msg = " 成功";
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "异常";
                }
                #endregion
                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW(" 查询所有商户的评价管理信息失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
        }

        public string GetUserContactByMerid(string MerId, int AccID, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 业务方法
            try
            {
                //1.调用业务层方法 根据ID查询
                List<Model.User_Contact_T> usr  = (from s in  base.DBSession.IUser_Contact_TDAL.GetListBy(u => u.AccID == AccID & u.ContactState == 1) where (from m in base.GetListBy(u => u.MerId == MerId) select m.MerSendAddId).Contains(s.MerAddId) select s).ToList();
                //2.判断是否登陆成功
                if (usr != null)
                {
                    result.BS = "1";
                    result.Msg = "查询成功！";
                    result.rows = usr;
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据！";
                }
                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("入参：" + AccID + "|" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统异常！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
        }

    }
}
